package com.sky.controller.user;


import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.UserDeliveryTimeService;
import com.sky.service.user.UserOrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Api(tags = "管理/售后")
@RequestMapping("/user/order")
@Slf4j
public class UserOrderController {

    @Autowired
    public UserDeliveryTimeService timeService;

    @Autowired
    public UserOrderService userOrderService;


    @ApiOperation("计算送达时间")
    @GetMapping("/getEstimatedDeliveryTime")
    public Result getUserDeliveryTime(Integer shopId, String customerAddress) {
        LocalDateTime userDeliveryTime = timeService.getUserDeliveryTime(shopId, customerAddress);
        return Result.success(userDeliveryTime);
    }

    @ApiOperation("下单支付")
    @PostMapping("/submit")//user/order/submit  为什么支付不了？
    public Result sumit(@RequestBody OrdersSubmitDTO submit) {
        OrderSubmitVO submitVO = userOrderService.sumit(submit);
        return Result.success(submitVO);

    }


    @ApiOperation("分页查询历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> getPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        PageResult pageResult = userOrderService.getPage(page, pageSize);
        return Result.success(pageResult);
    }

    @ApiOperation(".查询可退款的订单")
    @GetMapping("/queryOrdersCheckStatus")
    public Result<PageResult> getByStatus(Integer status) {
        PageResult pageResult = userOrderService.getByStatus(status);
        return Result.success(pageResult);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{orderId}")
    public Result<OrderVO> getOrderDetali(@PathVariable("orderId") Integer orderId) {

        OrderVO orderVO = userOrderService.getOrderDetali(orderId);
        return Result.success(orderVO);
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel/{id}")
    public Result quXiao(@PathVariable("id") Integer id) {
        userOrderService.quXiao(id);
        return Result.success("取消成功");
    }

    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result oneRepetition(@PathVariable("id") Integer id) {

        userOrderService.oneRepetition(id);
        return Result.success("下单成功");
    }
}
